//
//  WithdrawController.m
//  LianShangBao
//
//  Created by 主事丫环HU on 2020/9/3.
//  Copyright © 2020 主事丫环. All rights reserved.
//

#import "WithdrawController.h"

@interface WithdrawController ()<UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UILabel *cardNumLabel;
@property (weak, nonatomic) IBOutlet UITextField *amountTextField;

@end

@implementation WithdrawController
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
}
- (void)setupUI{
    self.title = @"提现";
    [self.leftbutton setImage:imgname(@"lefticon") forState:(UIControlStateNormal)];
     self.view.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    
    self.amountTextField.delegate = self;
    
    if ([self.model.isRealName isEqualToString:@"0"]) {// 还没有实名认证
        self.cardNumLabel.text = @"未添加银行卡，无法提现";
    } else {
        self.cardNumLabel.text = self.model.cardNumber;
    }
    self.amountTextField.placeholder = [NSString stringWithFormat:@"%@可提现",self.model.balance];
}
- (IBAction)allButtonClick:(UIButton *)sender {
    self.amountTextField.text = self.model.balance;
}
- (IBAction)confirmButtonClick:(UIButton *)sender {

    if ([self.model.isRealName isEqualToString:@"0"]) {// 还没有实名认证
        ShowToastWithText(@"请前往实名认证并添加银行卡");
        return;
    }
    if ([self.amountTextField.text isEqualToString:@""]) {
        ShowToastWithText(@"请输入提现金额");
        return;
    }
    if (self.amountTextField.text.doubleValue > self.model.balance.doubleValue || self.model.balance.doubleValue == 0) {
        ShowToastWithText(@"余额不足");
        return;
    }
    if (self.amountTextField.text.doubleValue <= 0) {
        ShowToastWithText(@"输入余额应大于0");
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"userId"] = USER_ID;
    param[@"money"] = self.amountTextField.text;
    param[@"name"] = self.model.name;
    param[@"cardNumber"] = self.model.cardNumber;
    param[@"card"] = self.model.cardBank;
    param[@"cardId"] = self.model.cardBankNum;
    [MBProgressHUD showHUDAddedTo:self.view  animated:YES];
    [NetWorkTool POST:withdraw param:param success:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [self.navigationController popViewControllerAnimated:YES];
    } other:^(id dic) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } fail:^(NSError *error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } needUser:YES];
}
#pragma UITextFieldDelegate
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    
    if (string.length == 0) {
        return YES;
    }
    NSString *checkStr = [textField.text stringByReplacingCharactersInRange:range withString:string];
    //正则表达式（只支持两位小数）
    NSString *regex = @"^\\-?([1-9]\\d*|0)(\\.\\d{0,2})?$";
    //判断新的文本内容是否符合要求
    return [self isValid:checkStr withRegex:regex];
}
- (BOOL) isValid:(NSString*)checkStr withRegex:(NSString*)regex
{
    NSPredicate *predicte = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    return [predicte evaluateWithObject:checkStr];
}


@end
